package com.example.guodufragment;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;


/**
 *
 * android 5.0 中的活动和碎片过渡建立在
 * 一些相对新的特性上，叫做过渡 在kitKat中引入
 * 过渡框架提供了一系列方便的API来在应用程序的
 * 不同UI状态间提供动画。这个框架围绕两个关键概念构建：场景和过渡。
 * 场景定义了应用程序UI给定的状态，过渡则定义了两个场景间的动画变化
 *
 *  捕获开始和结束场景中每个视图的状态
 *  创建一个动画绘制者(Animator),基于一个场景到另一个场景需要动画的视图的不同
 *
 *
* */
public class MainActivity extends Activity {

    int mStackLevel = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = findViewById(R.id.new_fragment);

        button.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                      addFragmentToStack();
                    }
                }
        );

        if(savedInstanceState == null){
           Fragment countingFragment = CountingFragment.newInstance(mStackLevel);
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.add(R.id.fragment1,countingFragment).commit();
        }else{
            mStackLevel = savedInstanceState.getInt("level");
        }

    }


    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("level",mStackLevel);
    }



    private void addFragmentToStack() {

        mStackLevel++;
        //实例化新的碎片
        Fragment newFragment = CountingFragment.newInstance(mStackLevel);
        //添加碎片到活动，并将其放入后退栈中

        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.setCustomAnimations(R.animator.fragment_slide_left_enter,R.animator.fragment_slide_left_exit,
            R.animator.fragment_slide_right_enter,
            R.animator.fragment_slide_right_exit);
        ft.replace(R.id.fragment1,newFragment);
        ft.addToBackStack(null);
        ft.commit();


    }

    public static class CountingFragment extends Fragment {

        int mNum;

        static CountingFragment newInstance(int num) {
            CountingFragment f = new CountingFragment();
            Bundle args = new Bundle();
            args.putInt("num", num);
            f.setArguments(args);
            return f;
        }

        /**
         * 在创建时获取实例的number参数
         *
         * */

        @Override
        public void onCreate(Bundle savedInstanState) {
            super.onCreate(savedInstanState);
           mNum= getArguments()!=  null ? getArguments().getInt("num"):1;

        }

        /**
         * 碎片的界面仅包含一个TextView,用于显示number
         * */
        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.fragment_stack, container, false);
            View tv = v.findViewById(R.id.text);
            ((TextView)tv).setText("Fragment #"+mNum);
            tv.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.gallery_thumb));
            return v;

        }
    }

}
